package com.bj58.mis.apm.server.logreceiver.controller;

import com.bj58.mis.apm.server.core.common.result.ResultBean;
import com.bj58.mis.apm.server.core.util.JsonUtil;
import com.bj58.mis.apm.server.logreceiver.service.PropertiesService;
import com.bj58.mis.apm.server.logreceiver.vo.ApmClientForWeb;
import com.bj58.mis.apm.server.logreceiver.vo.ApmPropertyForClient;
import com.bj58.mis.apm.server.logreceiver.vo.ApmPropertyForWeb;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author: wangxw
 * @Date: 2018/7/26 14:01
 */
@RestController
@RequestMapping("/properties")
public class PropertiesController {

    @Resource
    private PropertiesService propertiesService;

    /**
     * 为客户端提供的获取参数服务
     * @param appName
     * @param hostName
     * @return
     */
    @RequestMapping("/get")
    public String getProperties(@RequestParam(name="appName", required = true) String appName, @RequestParam(name="hostName", required = true) String hostName){
        List<ApmPropertyForClient> list = propertiesService.queryProperties(appName, hostName);
        return JsonUtil.toJson(list);
    }

    /**
     * 为客户端提供的上传参数服务
     * @param propList
     */
    @RequestMapping("/post")
    public void saveProperties(@RequestBody List<Map<String, String>> propList){
        if(propList != null && propList.size() > 0){
            propertiesService.saveProperties(propList);
        }
    }

    /**
     * 查询client列表
     * @return
     */
    @RequestMapping("/clients")
    public ResultBean<List<ApmClientForWeb>> queryClients() {
       List<ApmClientForWeb> list = propertiesService.queryClient();
       return ResultBean.success(list);
    }

    /**
     * 查询配置列表
     * @param appName
     * @param hostName
     * @return
     */
    @RequestMapping("/settings")
    public ResultBean<List<ApmPropertyForWeb>> queryProperties(@RequestParam(name="appName", required = true) String appName, @RequestParam(name="hostName", required = true) String hostName) {
        return ResultBean.success(propertiesService.querySettings(appName, hostName));
    }

    /**
     * 保存配置属性
     * @param list
     */
    @RequestMapping("/save")
    public void saveSettings(@RequestBody List<ApmPropertyForWeb> list){
        propertiesService.save(list);
    }
}
